// pages/http/http.js
Page({
  getNowFormatDate: function(){
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
  },
  http: function(event){

    let appSecret = '96a24eec-5b82-4ccf-8142-d7bb91da83d8'

    let goodsIds = 1311133
    let unionId = 999684145825
    let signMethod = 'md5'
    let timestamp = this.getNowFormatDate()
    let type = '1'
    let v = '1.0'
    let version = '1.0'

    let signStr = `goodsIds${goodsIds}signMethod${signMethod}timestamp${timestamp}type${type}unionId${unionId}v${v}version${version}`
    let sign = appSecret + signStr +appSecret

    console.log(timestamp)

    let utilMd5 = require('md5')
    let signMd5 = utilMd5.hexMD5(sign)
    let signMd5Up = signMd5.toUpperCase()
    console.log(signMd5Up)

    wx.cloud.callFunction({
      name: 'http',
      data: {
        unionId,
        goodsIds,
        type,
        signMethod,
        timestamp,
        version,
        sign: signMd5Up,
        v
      }
    }).then( res => {
      console.log(res)
    })

    // wx.cloud.callFunction({
    //   name: 'http'
    // }).then( res => {
    //   console.log(res)
    // })
  }
})